package com.yingxue.lesson.service.impl;

import com.github.pagehelper.PageHelper;
import com.yingxue.lesson.dao.SysRoleDao;
import com.yingxue.lesson.entity.SysRole;
import com.yingxue.lesson.service.SysRolePermissionService;
import com.yingxue.lesson.service.SysRoleService;
import com.yingxue.lesson.service.SysUserRoleService;
import com.yingxue.lesson.utils.PageUtils;
import com.yingxue.lesson.vo.req.AddRoleReqVO;
import com.yingxue.lesson.vo.req.RolePageReqVO;
import com.yingxue.lesson.vo.req.RolePermissionOperationReqVO;
import com.yingxue.lesson.vo.resp.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (SysRole)表服务实现类
 *
 * @author makejava
 * @since 2022-04-05 21:40:53
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleDao sysRoleDao;

    @Resource
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysRole queryById(String id) {
        return this.sysRoleDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole insert(SysRole sysRole) {
        this.sysRoleDao.insert(sysRole);
        return sysRole;
    }

    /**
     * 修改数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole update(SysRole sysRole) {
        this.sysRoleDao.update(sysRole);
        return this.queryById(sysRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.sysRoleDao.deleteById(id) > 0;
    }

    /**
     * 分页查询所有数据
     * @param vo
     * @return
     */
    @Override
    public PageVO<SysRole> pageInfo(RolePageReqVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<SysRole> sysRoleList = sysRoleDao.queryAll(vo);
        PageVO<SysRole> pageVO = PageUtils.getPageVO(sysRoleList);
        return pageVO;
    }

    /**
     * 新增角色
     * @param addRoleReqVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysRole addRole(AddRoleReqVO addRoleReqVO) {
        // 保存角色表信息
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(addRoleReqVO,sysRole);
        sysRole.setId(UUID.randomUUID().toString());
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRoleDao.insertSelective(sysRole);

        // 保存角色和权限关联关系表
        List<String> permissionList = addRoleReqVO.getPermissions();
        if(!CollectionUtils.isEmpty(permissionList)){
            RolePermissionOperationReqVO operationReqVO = new RolePermissionOperationReqVO();
            operationReqVO.setRoleId(sysRole.getId());
            operationReqVO.setPermissionIds(permissionList);
            sysRolePermissionService.addRolePermission(operationReqVO);
        }
        return sysRole;
    }

    @Override
    public List<SysRole> selectAll() {
        List<SysRole> sysRoles = sysRoleDao.queryAll(new RolePageReqVO());
        return sysRoles;
    }

    @Override
    public List<String> getNamesByUserId(String userId) {
        List<String> roleIdsByUserId = sysUserRoleService.getRoleIdsByUserId(userId);
        if(roleIdsByUserId.isEmpty()){
            return null;
        }
        return sysRoleDao.selectNamesByIds(roleIdsByUserId);
    }
}
